package com.wanpinghui.supplier.user.order.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.order.bean.OrderGoodsFormatBean;
import com.wanpinghui.wphlibrary.model.order.bean.OrderGoodsItemBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;

import java.util.List;

/**
 * 功能说明：
 * Created by caitiangui
 * 创建时间： 2017/12/21.
 */

public class OrderGoodsAdapter extends BaseRecyclerViewAdapter<OrderGoodsItemBean, OrderGoodsAdapter.OrderGoodsViewHolder> {

    public OrderGoodsAdapter(Context context) {
        super(context);
    }

    @Override
    public OrderGoodsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_goods, parent, false);
        return new OrderGoodsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderGoodsViewHolder holder, int position) {
        //super.onBindViewHolder(holder, position);
        OrderGoodsItemBean bean = datas.get(position);
        Glide.with(context).load(bean.getMain_img()).fitCenter().placeholder(R.mipmap.loading_140x140).error(R.mipmap.loading_140x140).diskCacheStrategy(DiskCacheStrategy.RESULT).into(holder.image);
        holder.tv_object_name.setText(bean.getName());
        holder.tv_count.setText("共 " + bean.getQuantity() + " 件     合计  ￥" + bean.getPrice());
        List<OrderGoodsFormatBean> orderGoodsFormatBeanList = bean.getOrderSpecItemList();
        if (orderGoodsFormatBeanList == null || orderGoodsFormatBeanList.size() <= 1) {
            holder.ll_more_format.removeAllViews();
        } else {
            holder.ll_more_format.removeAllViews();
            for (int i = 0; i < orderGoodsFormatBeanList.size(); i++) {
                holder.ll_more_format.addView(addFormat(orderGoodsFormatBeanList.get(i)));
            }
        }

    }

    private View addFormat(OrderGoodsFormatBean bean) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_format, null);
        TextView tv_goods_format = (TextView) view.findViewById(R.id.tv_goods_format);
        TextView tv_count = (TextView) view.findViewById(R.id.tv_count);
        if (bean.is_consult()) {
            tv_goods_format.setText(bean.getSpec_value() + "   议价");
        } else {
            tv_goods_format.setText(bean.getSpec_value() + "   ￥" + bean.getPrice());
        }
        tv_count.setText("x" + bean.getQuantity());
        return view;
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class OrderGoodsViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView tv_object_name;
        TextView tv_count;
        LinearLayout ll_more_format;

        OrderGoodsViewHolder(View itemView) {
            super(itemView);
            ll_more_format = (LinearLayout) itemView.findViewById(R.id.ll_more_format);
            image = (ImageView) itemView.findViewById(R.id.image);
            tv_object_name = (TextView) itemView.findViewById(R.id.tv_object_name);
            tv_count = (TextView) itemView.findViewById(R.id.tv_count);
        }
    }
}
